package com.zlx.sharelive.activity;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.zlx.module_base.base_ac.BaseAc;
import com.zlx.module_base.base_api.res_data.AuthenticationBean;
import com.zlx.module_base.base_api.res_data.UploadBean;
import com.zlx.module_base.base_api.util.ApiUtil;
import com.zlx.module_base.base_util.SPUtil;
import com.zlx.module_network.api1.livedata.BaseObserver;
import com.zlx.module_network.api1.livedata.BaseObserverCallBack;
import com.zlx.module_network.bean.ApiResponse;
import com.zlx.sharelive.R;
import com.zlx.sharelive.utils.Glide4Engine;
import com.zlx.sharelive.utils.GlideImgManager;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import me.bzcoder.mediapicker.SmartMediaPicker;
import me.bzcoder.mediapicker.config.MediaPickerEnum;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * 女神认证
 */
public class GoddessUpAc extends BaseAc {

    @BindView(R.id.iv_path)
    ImageView iv_path;

    private String path;
    private String video_path;
    private List<File> fileList = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.ac_goddess_up;
    }


    @Override
    public void initViews() {
        super.initViews();

        path = getIntent().getStringExtra("path");

        GlideImgManager.glideLoaderFile(GoddessUpAc.this, new File(path), iv_path, 1);
    }

    @OnClick({R.id.iv_back, R.id.bt_preview, R.id.bt_recording, R.id.bt_to_examine})
    public void setOnClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.bt_preview:
                startActivity(new Intent(this, GoddessVideoAc.class)
                        .putExtra("path", path));
                break;
            case R.id.bt_recording:
                //重新录制
                SmartMediaPicker.builder(this)
                        //最大图片选择数目 如果不需要图片 将数目设置为0
                        .withMaxImageSelectable(0)
                        //最大视频选择数目 如果不需要视频 将数目设置为0
                        .withMaxVideoSelectable(0)
                        //图片选择器是否显示数字
                        .withCountable(true)
                        //最大视频长度
                        .withMaxVideoLength(15 * 1000)
                        //最大视频文件大小 单位MB
                        .withMaxVideoSize(1)
                        //最大图片高度 默认1920
                        .withMaxHeight(1920)
                        //最大图片宽度 默认1920
                        .withMaxWidth(1920)
                        //最大图片大小 单位MB
                        .withMaxImageSize(5)
                        //设置图片加载引擎
                        .withImageEngine(new Glide4Engine())
                        //前置摄像头拍摄是否镜像翻转图像
                        .withIsMirror(false)
                        //弹出类别，默认弹出底部选择栏，也可以选择单独跳转
                        .withMediaPickerType(MediaPickerEnum.CAMERA)
                        .build()
                        .show();
                break;
            case R.id.bt_to_examine:
                //提交审核
                updateVideo();
                break;
        }
    }

    /**
     * 上传视频
     */
    private void updateVideo() {
        showLoading();
        fileList.clear();
        fileList.add(new File(path));
        ApiUtil.getVideoApi().upload(filesToMultipartBody(fileList)).observe(this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<UploadBean>>() {
            @Override
            public void onSuccess(ApiResponse<UploadBean> data) {
                showSuccess();
                video_path = data.getData().getInfo().get(0).getUrl();
                authentication();

            }
        }));
    }

    /**
     * 女神认证
     */
    private void authentication() {
        ApiUtil.getUserApi().authentication(SPUtil.getToken(GoddessUpAc.this), video_path).
                observe(this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<AuthenticationBean>>() {
                    @Override
                    public void onSuccess(ApiResponse<AuthenticationBean> data) {
                        //成功
                        Toast.makeText(GoddessUpAc.this, "已提交审核", Toast.LENGTH_SHORT).show();
                        finish();
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();

                    }
                }));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        List<String> resultData = SmartMediaPicker.getResultData(this, requestCode, resultCode, data);
        if (resultData != null && resultData.size() > 0) {
            path = String.valueOf(resultData.toArray()[0]);
            GlideImgManager.glideLoaderFile(GoddessUpAc.this, new File(path), iv_path, 1);
        }
    }

    public static MultipartBody filesToMultipartBody(List<File> files) {
        MultipartBody.Builder builder = new MultipartBody.Builder();

        for (File file : files) {
            // TODO: 16-4-2  这里为了简单起见，没有判断file的类型
            RequestBody requestBody = RequestBody.create(MediaType.parse("video/mp4"), file);
            builder.addFormDataPart("file", file.getName(), requestBody);
        }

        builder.setType(MultipartBody.FORM);
        MultipartBody multipartBody = builder.build();
        return multipartBody;
    }


}
